$(document).ready(function(){
	// ========== Slideshow =================
	var slideNumb = $('.slideshow li').length;
	if(slideNumb<=1){
		playSlide = 'off';
		//clearInterval(intervalCall);
	};
	sliderBegin();
	
	
	// ========== Tabbed content ============
	tabsBegin();
	
	$('.tabbed .tabs_hold a').each(function(){
		$(this).click(function(event){
			event.preventDefault();
                        //objavljeni, komentarisani, najpopularniji
                        var clikedTab = $(this).attr('id')
                        if (clikedTab=="objavljeni" || clikedTab=="komentarisani" || clikedTab=="najpopularniji") {
                            $.cookie('profil_trenutna_galerija', clikedTab);
                        } else {
                            $.cookie('nalog_trenutna_galerija', clikedTab);
                        }
                        
			$(this).parent().find('.current').removeClass('current');
			$(this).addClass('current');
			var tabTarg = $(this).attr('href');
			var tabMethod = $(this).attr('rel');
			if(tabMethod=='inpage'){showTab(tabTarg)}
				else {showAjaxTab(tabTarg)};
		});
	});
	
	
	
	// ========== MODAL LIGHTBOXES =========
	/*create lightbox elements and load AJAX CONTENT*/
	$('.modal_act').live('click',function(event){
		event.preventDefault();
		modalCreate();
		var modalTarg = $(this).attr('href');
		fillAjaxModal(modalTarg);
	});
	
	/*create lightbox elements and load IFRAME*/
	$('.modal_page').live('click',function(event){
		event.preventDefault();
		modalCreate();
		var modalTarg = $(this).attr('href');
		var modalTriger = $(this);
		fillIFrameModal(modalTarg, modalTriger);
	});	
	
	/*create lightbox elements and load HIDDEN CONTENT*/
	$('.modal_cont').live('click',function(event){
		event.preventDefault();
		modalCreate();
		var modalTarg = $(this).attr('href');
		fillHiddenModal(modalTarg);
	});
	
	/*close light box*/
	$('.modal .mod_close').live('click',function(event){
		event.preventDefault();
		$('.modal').hide(300, function(){$(this).remove()});
		$('.modal_overlay').delay(400).fadeOut(300, function(){$(this).remove()});
	});
	$('.modal_overlay').live('click',function(event){
		event.preventDefault();
		if(event.target == this) {
			$('.modal').hide(300, function(){$(this).remove()});
			$('.modal_overlay').delay(400).fadeOut(300, function(){$(this).remove()});
		} else {
			event.stopPropagation();
		};
		
	});
	
	/*create modal lightbox and overlay*/
	function modalCreate(){
		$('body').append('<div class="modal_overlay"><div class="modal"><div class="mod_caption"><span></span><a class="mod_close" href="#">Zatvori</a></div><div class="popup_holder"></div></div></div>');
		$('.modal_overlay').fadeIn(200, function(){
			$('.modal_overlay').addClass('loading');
		});
	};
	
	/*Load page to modal iframe*/
	function fillIFrameModal(frModPath, modalTriger){
		var frameWidth = modalTriger.attr('rel').split('x')[0];
		var frameHeight = modalTriger.attr('rel').split('x')[1];
		
		$('.popup_holder').html('<iframe width="'+frameWidth+'" height="'+frameHeight+'" class="modal_frame" frameborder="0"></iframe>');
		$('.popup_holder iframe').attr('src',frModPath);
		
		$('.modal').show();
		var contWidth = $('.modal').children('.popup_holder').outerWidth();
		var contMargX = contWidth/2;
		$('.modal_overlay').removeClass('loading');
		$('.modal').hide();
		$('.modal').css({
			'width':contWidth+'px',
			'margin-left':'-'+contMargX+'px'
		});
		$('.modal').show(300);
		
	};
	
	/*Load ajax content to modal */
	function fillAjaxModal(ajModPath){
		$('.popup_holder').load(ajModPath, function(){
			$('.modal').show();
			var contWidth = $('.modal').children('.popup_holder').outerWidth();
			var contMargX = contWidth/2;
			$('.modal_overlay').removeClass('loading');
			$('.modal').hide();
			$('.modal').css({
				'width':contWidth+'px',
				'margin-left':'-'+contMargX+'px'
			});
			$('.modal').show(300);
			
		});
	};
	
	/*Load ajax content to modal */
	function fillHiddenModal(hidModPath){
		$(hidModPath).clone().appendTo('.popup_holder');
		$('.modal').show();
		var contWidth = $('.modal').children('.popup_holder').outerWidth();
		var contMargX = contWidth/2;
		$('.modal_overlay').removeClass('loading');
		$('.modal').hide();
		$('.modal').css({
			'width':contWidth+'px',
			'margin-left':'-'+contMargX+'px'
		});
		$('.modal').show(300);
	};
	
	
	
	
	
});


//***** SLIDESHOW FUNCTIONS *****				
var intervalCall;
var timeOut = 5000; //pause time for slideshow in miliseconds
var playSlide = 'on'; //turn on/off slideshow		
		
function sliderBegin(){
	$('.slideshow li:first').fadeIn(300);
	if (playSlide=='on'){intervalCall = setInterval("slideNext()",timeOut);}
};
			
function slideNext(){
	clearInterval(intervalCall);
	$('.slideshow li:first').fadeOut(400, function(){
		$('.slideshow li:last').after($('.slideshow li:first'));
		$('.slideshow li:first').fadeIn(800);
		if (playSlide=='on'){intervalCall = setInterval("slideNext()",timeOut);}
	})
};
				
function slidePrev(){
	clearInterval(intervalCall);
	$('.slideshow li:first').fadeOut(300, function(){
		$('.slideshow li:first').before($('.slideshow li:last'));
		$('.slideshow li:first').fadeIn(500);
		if (playSlide=='on'){intervalCall = setInterval("slideNext()",timeOut);}
	})
};



//***** TABS FUNCTIONS *****
function tabsBegin(){
	$('.tabbed .tabs_cont_hold .tab_cont').hide();
	var tabTarg = $('.tabbed .tabs_hold a.current').attr('href');
	var tabMethod = $('.tabbed .tabs_hold a.current').attr('rel');
	if(tabMethod=='inpage'){showTab(tabTarg);}
		else {showAjaxTab(tabTarg);};
};

function showTab(tabTarg){
	/*$('.tabbed .tabs_cont_hold .tab_cont').fadeOut(100, function(){
		$('.tabbed .tabs_cont_hold').find(tabTarg).delay(100).fadeIn(200);
	});*/
	$('.tabbed .tabs_cont_hold .tab_cont').fadeOut(100);
	$('.tabbed .tabs_cont_hold').find(tabTarg).delay(110).fadeIn();
};

function showAjaxTab(tabTarg){
};


